package se.sudoku;

import java.util.Random;

import android.app.Activity;
import se.sudoku.SudokuGame;
import android.content.Context;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

@SuppressWarnings("deprecation")
public class SudokuAttacks{
	
	public static final int INCREMENTDIGITS = 1;
	public static final int CRAZY = 2;
	public static final int REMOVEDIGIT = 3;
	public static final int HIDEBLOCK = 4;
	public static final int NUMBEROFATTACKS = 4;
	
	Context context;
	Activity activity;
	PopupWindow attackMode;
	LinearLayout attackField;
	MyCount countDown, indicatorCountDown;
	int chosenAttack;
	ImageView block, indicator;
	Ringtone ringTone;
	Vibrator vib;
	int width, widthDiv3;
	
	//constructor
	public SudokuAttacks(Context c, int sw){
		activity = (Activity) c;
		context = c;
		width = sw;
		widthDiv3 = sw/3;
	}
	
	
	
	
	public void incrementDigits(){
		
		int randomNumber;
		Random rand = new Random();
		randomNumber = rand.nextInt(8)+1; //1-9
			
		for (int i = 0 ; i < 81 ; i++) {
			if(SudokuGame.puzzle[i]!=0){
				SudokuGame.puzzle[i]+=randomNumber;
				SudokuGame.puzzle[i]=(SudokuGame.puzzle[i]%9)+1;
			}
			SudokuGame.facit[i]+=randomNumber;
			SudokuGame.facit[i]=(SudokuGame.facit[i]%9)+1;
		}
	}
	
	//crazy
	public void crazy(){
		Uri defaultUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
		ringTone = RingtoneManager.getRingtone(context, defaultUri);
		vib = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
		long[] vibPattern = {100L, 500L, 200L, 300L, 100L};
		
		ringTone.play();
		vib.vibrate(vibPattern, 2);
		
		countDown = new MyCount(10000, 1000, 2);
		countDown.start();
	}
	
	public void removeDigit(int pos){
		//don't remove original digits
		if(SudokuGame.used[pos]==0 && SudokuGame.puzzle[pos] != 0){
			//remove value and lower number of used tiles
			SudokuGame.puzzle[pos] = 0;
			SudokuGame.numberOfTilesUsed--;
		}
	}
	
	public void hideBlock(int pos){
		int x = (pos%3)*widthDiv3;
		int y= (int) (Math.floor(pos/3)*widthDiv3);	
		
		block = (ImageView) activity.findViewById(R.id.attack_block);
		block.setLayoutParams(new AbsoluteLayout.LayoutParams(widthDiv3, widthDiv3, x, y));
		block.setVisibility(View.VISIBLE);
		
		countDown = new MyCount(15000, 1000, 4);
		countDown.start();
	}
	
	//get attacked indicator
	public void showAttackedIndicator(){
		indicator = (ImageView) activity.findViewById(R.id.attack_indicator);
		indicator.setVisibility(View.VISIBLE);
		
		indicatorCountDown = new MyCount(1000, 1000, 0);
		indicatorCountDown.start();
	}
	
	
	
	public void startAttackMode(int attack){
		chosenAttack=attack;
		
		attackMode = new PopupWindow(View.inflate(context, R.layout.attackmode, null), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);
		attackField = (LinearLayout)attackMode.getContentView().findViewById(R.id.attackmode_field);
		attackField.setLayoutParams(new LinearLayout.LayoutParams(width,width));
		attackField.setOnTouchListener(attackModeListener);
		toggleWin(attackMode);
	}
	
	
	public void toggleWin(PopupWindow win){  	
    	if(!win.isShowing()){
    		win.showAtLocation(activity.findViewById(R.id.sudokuview), Gravity.CENTER, 0, 0);
    	}
    	else{
    		win.dismiss();
    	}
    }
	
	public int calcTilePosition(float x, float y){
		//error control
		if(x>=width){x=width-1;}
		if(y>=width){y=width-1;}
	
		return (int) (Math.floor((y/width)*9)*9 + Math.floor((x/width)*9));
	}
	
	public int calcBlockPosition(float x, float y){
		//error control
		if(x>=width){x=width-1;}
		if(y>=width){y=width-1;}
	
		return (int) (Math.floor((y/width)*3)*3 + Math.floor((x/width)*3));
	}
	
	//attack function
	public void getAttacked(int attack, int pos){
		switch(attack){
		case INCREMENTDIGITS:
			incrementDigits();
			break;
		case CRAZY:
			crazy();
			break;
		case REMOVEDIGIT:
			removeDigit(pos);
			break;	
		case HIDEBLOCK:
			hideBlock(pos);
			break;
		}
		showAttackedIndicator();
	}
	
	//hands out attacks
	public int getRandomAttack(){
		Random rand = new Random();
		return rand.nextInt(NUMBEROFATTACKS)+1; //1-NUMBEROFATTACKS
	}
	
	//attack
	public void Attack(int attackNbr){
		//Attacks
    	switch(attackNbr){
    	//Increment
    	case(INCREMENTDIGITS):
    		SudokuGame.setAttack(INCREMENTDIGITS,0);
    		break;
    	//crazy
    	case(CRAZY):
    		SudokuGame.setAttack(CRAZY,0);
    		break;
    	//removeDigit
		case(REMOVEDIGIT):
			startAttackMode(REMOVEDIGIT);
			break;
		//hideBlock
		case(HIDEBLOCK):
			startAttackMode(HIDEBLOCK);
			break;
		}
	}
	
	
	//ATTACKFIELD LISTERNER
	private OnTouchListener attackModeListener = new OnTouchListener() {            
	    public boolean onTouch(View v, MotionEvent e){
	    	
	    	//remove popup by pressing the outside
	    	if(v==attackField){
	    		switch(chosenAttack){
	    			//removeDigit
	    			case REMOVEDIGIT:
	    				SudokuGame.setAttack(REMOVEDIGIT, calcTilePosition(e.getX(), e.getY()));
	    				break;
	    			//hideBlock
	    			case HIDEBLOCK:
	    				SudokuGame.setAttack(HIDEBLOCK, calcBlockPosition(e.getX(), e.getY()));
	    				break;
	    		}
	    		toggleWin(attackMode);
	    	}
			return false;
	    }
	};
	
	
	
	public class MyCount extends CountDownTimer{
		
		int attack;

		public MyCount(long millisInFuture, long countDownInterval, int a) {
			super(millisInFuture, countDownInterval);
			
			attack=a;
		}

		@Override
		public void onFinish() {
			
			switch(attack){
			case(0):
				indicator.setVisibility(View.GONE);
				break;
			case(CRAZY):
				ringTone.stop();
				vib.cancel();
				break;
			case(HIDEBLOCK):
				block.setVisibility(View.GONE);
				break;
			}
		}

		@Override
		public void onTick(long millisUntilFinished) {
		}
	}

	
}
